package com.ce.source.analysis.lock;

/**
 * @author ：前世小和尚（xiaopingzhang8@creditease.cn）
 * @Description ：synchronized修饰static方法和普通方法的区别
 * <p>
 * synchronized修饰静态方法和非静态方法的区别
 * </p>
 * <pre>
 *     修饰非静态方法拿到的是对象锁
 *          注意：一个对象只能持有一把对象锁，在没有执行完成之后不能执行synchronized修饰的非静态方法，但是可以执行无锁方法
 *     修饰静态方法拿到的是类锁
 *          注意：不同对象的该方法互斥，不影响别的方法
 * </pre>
 * @ClassName ：SynchronizedRunnable
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2018/1/30 10:17
 */

public class SynchronizedRunnable {
    public synchronized void printNum() {
        while (true) {
            System.out.print(Thread.currentThread().getName() + "：");
            for (int i = 0; i < 25; i++) {
                System.out.print(i + " ");
            }
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void printNum2() {
        while (true) {
            System.out.print(Thread.currentThread().getName() + "：");
            for (int i = 0; i < 25; i++) {
                System.out.print("哼" + " ");
            }
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void printNum3() {
        while (true) {
            System.out.print(Thread.currentThread().getName() + "：");
            for (int i = 0; i < 25; i++) {
                System.out.print("哈" + " ");
            }
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static synchronized void printNum4() {
        while (true) {
            System.out.print(Thread.currentThread().getName() + "：");
            for (int i = 0; i < 25; i++) {
                System.out.print("二将" + " ");
            }
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static synchronized void printNum5() {
        while (true) {
            System.out.print(Thread.currentThread().getName() + "：");
            for (int i = 0; i < 25; i++) {
                System.out.print("啦啦" + " ");
            }
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void printNum6() {
        synchronized (this) {
            while (true) {
                System.out.print(Thread.currentThread().getName() + "：");
                for (int i = 0; i < 25; i++) {
                    System.out.print("呼呼" + " ");
                }
                System.out.println();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args){
        final SynchronizedRunnable sr = new SynchronizedRunnable();
        final SynchronizedRunnable sr2 = new SynchronizedRunnable();
       /* new Thread("thread-1") {
            @Override
            public void run() {
                sr.printNum();
            }
        }.start();

        new Thread("thread-2") {
            @Override
            public void run() {
                sr.printNum2();
            }
        }.start();

        new Thread("thread-3") {
            @Override
            public void run() {
                sr.printNum3();
            }
        }.start();
        new Thread("thread-4") {
            @Override
            public void run() {
                sr.printNum4();
            }
        }.start();
        new Thread("thread-5") {
            @Override
            public void run() {
                sr2.printNum4();
            }
        }.start();*/

        new Thread("thread_6") {
            @Override
            public void run() {
                printNum4();
            }
        }.start();

        new Thread("thread_7") {
            @Override
            public void run() {
                sr.printNum3();
            }
        }.start();

        Thread thread8 = new Thread("thread_8"){
            @Override
            public void run() {
                sr.printNum2();
            }
        };
        thread8.start();
        try {
            thread8.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
